package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.flyjingfish.openimagelib.OpenImage
import com.flyjingfish.openimagelib.enums.MediaType
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.RaceVoucherResponse
import com.gexiaobao.pigeon.databinding.ItemRaceProofBinding

/**
 * @Author      : hxw
 * @Date        : 2024/3/27 10:08
 * @Describe    : 比赛规程adapter
 */
class RaceProofAdapter(data: ArrayList<RaceVoucherResponse.ListBean>) :
    BaseQuickAdapter<RaceVoucherResponse.ListBean, DataBindBaseViewHolder>(R.layout.item_race_proof, data) {

    private var onProofClickListener: OnProofClickListener? = null

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: RaceVoucherResponse.ListBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemRaceProofBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        val ivBusiness = holder.getView<ImageView>(R.id.ivItemProofBusinessLicense)
        val llBusiness = holder.getView<LinearLayout>(R.id.llItemProofBusinessLicense)
        val url = item.businessLicense
        llBusiness.setOnClickListener {
            OpenImage.with(context)
                .setClickImageView(ivBusiness)
                .setSrcImageViewScaleType(ImageView.ScaleType.CENTER_CROP, true)
                .setImageUrl(url, MediaType.IMAGE).show()
        }
        holder.setGone(R.id.llRelatedDocument, item.isEmpty())

        holder.itemView.apply {
            binding.rvItemRaceProofCheckpoint.adapter = null
            if (item.isEmpty() || item.list.size == 0) {
                holder.setGone(R.id.rvItemGoodsOrderDetail, true)//隐藏
                return
            }
            holder.setGone(R.id.rvItemRaceProofCheckpoint, false)//显示
            val mAdapter = RelatedDocumentAdapter(item.list)
            binding.rvItemRaceProofCheckpoint.apply {
                layoutManager = GridLayoutManager(context, 2)
                adapter = mAdapter
                mAdapter.notifyDataSetChanged()
            }
            mAdapter.setOnRelatedClickListener(object : RelatedDocumentAdapter.OnRelatedClickListener {
                override fun onItemClick(position: Int, path: String, name: String) {
                    onProofClickListener?.onItemClick(
                        holder.layoutPosition,
                        position,
                        path,
                        name
                    )
                }
            })
        }
    }

    fun setOnProofClickListener(listener: OnProofClickListener?) {
        onProofClickListener = listener
    }

    interface OnProofClickListener {
        fun onItemClick(positionParent: Int, positionChild: Int, path: String, name: String)
    }
}